package addition.nio;

import addition.utils.FileUtils;
import org.junit.Test;

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class TransferToDemo {

    @Test
    public void test1() {

        /*
         * 改进后的 transferTo / transferFrom
         * 单次最大传输数据为 2GB
         */

        try (FileChannel inChannel = FileChannel.open(Paths.get(FileUtils.getRootFilePath("wallhaven-z85gyw.jpg")), StandardOpenOption.READ);
             FileChannel outChannel = FileChannel.open(Paths.get("wallhaven-copy.jpg"), StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {

            // 剩余大小
            long left = inChannel.size();
            while (left > 0) {
                left -= inChannel.transferTo((inChannel.size() - left), left, outChannel);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
